﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VASJ.Model;
using System.Web.Security;
using VASJ.Service.Library;
using VASJ.Service.Library.Manager;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        #region "AcpInetRoleProvider"
        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// AddUsersToRoles
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            Roles.Provider.AddUsersToRoles(usernames, roleNames);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// CreateRole
        /// </summary>
        /// <param name="roleName"></param>
        public void CreateRole(string roleName)
        {
            Roles.Provider.CreateRole(roleName);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// DeleteRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="throwOnPopulatedRole"></param>
        /// <returns></returns>
        public bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            return Roles.Provider.DeleteRole(roleName, throwOnPopulatedRole);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// FindUsersInRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="usernameToMatch"></param>
        /// <returns></returns>
        public string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            return Roles.FindUsersInRole(roleName, usernameToMatch);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// GetAllRoles
        /// </summary>
        /// <param name="projectType"></param>
        /// <returns></returns>
        public string[] GetAllRoles(string projectType)
        {
            return new RoleManager(projectType).GetAllRoles().ToArray();
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// GetRolesForUser
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public string[] GetRolesForUser(string projectType, string username)
        {
            return new RoleManager(projectType).GetRolesForUser(username);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// GetRolesForUserInProject
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public string[] GetRolesForUserInProject(string ApplicationName, int projectId, string username)
        {
            return new RoleManager(ApplicationName).GetRolesForUser(projectId, username);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// Author:Hoang Van Tai
        /// GetRolesForUserPS????
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public string[] GetRolesForUserPS(string projectType, string username)
        {
            return new RoleManager(projectType).GetRolesForUserPS(username);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// GetUsersInRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public string[] GetUsersInRole(string ApplicationName, string roleName)
        {
            List<string> lstUser = new List<string>();

            string[] uir = Roles.Provider.GetUsersInRole(roleName);

            var list_uip = (new Common(ApplicationName)).GetListUserNameInApplication();

            for (int i = 0; i < uir.Length; i++)
            {
                if (list_uip.Contains(uir[i].ToLower())) lstUser.Add(uir[i]);
            }

            return lstUser.ToArray();
        }


        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// GetUsersInRoleForProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public string[] GetUsersInRoleForProject(string ApplicationName, int projectId,string roleName,bool include_via_org = true)
        {
            string[] ret = null;

            UserManager um = new UserManager(ApplicationName);
            ret = um.GetUserInRole(projectId, roleName, include_via_org);

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// IsUserInRole
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="userName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool IsUserInRole(string projectType, string userName, string roleName)
        {
            return Roles.Provider.IsUserInRole(userName, roleName) || new UserManager(projectType).IsUserInRole(userName, roleName);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// IsRoleInUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectId"></param>
        /// <param name="userName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool IsRoleOfUserInProject(string ApplicationName, int projectId, string userName, string roleName)
        {
            return new UserManager(ApplicationName).IsRoleOfUserInProject(projectId, userName, roleName);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// RemoveUsersFromRoles
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public void RemoveUsersFromRoles(string projectType, string[] usernames, string[] roleNames)
        {
            new UserManager(projectType).RemoveUsersFromRoles(usernames, roleNames);

            Roles.Provider.RemoveUsersFromRoles(usernames, roleNames);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// RoleExists
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool RoleExists(string roleName)
        {
            return Roles.Provider.RoleExists(roleName);
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get List Role
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public List<Role> GetAllRole(string ApplicationName)
        {
            try
            {
                RoleManager rm = new RoleManager(ApplicationName);
                return rm.GetAllRole();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Role.GetAllRole error {0}", ex);
                return null;
            }
        }
        #endregion

        #region "AcpInetRoleType"

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list Role for ispm, prjtype
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ispm"></param>
        /// <param name="prjtype"></param>
        /// <returns></returns>
        public List<Role> GetRoleList(string ApplicationName, string username, bool ispm = false, int prjtype = 0)
        {
            try
            {
                RoleManager rm = new RoleManager(ApplicationName);
                return rm.GetRoleList(username, ispm, prjtype);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Role.GetRoleList error {0}", ex);
                return null;
            }
        }
        #endregion


    }
}